<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/admin' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>管理员管理</el-breadcrumb-item>
    </el-breadcrumb>

    <h3 style="margin: 20px 0;">管理员管理</h3>

    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="id" label="ID" width="60" align="center"></el-table-column>
      <el-table-column prop="username" label="用户名" width="200" align="center"></el-table-column>
      <el-table-column prop="nickname" label="昵称" width="200" align="center"></el-table-column>
      <el-table-column prop="phone" label="手机号码" width="200" align="center"></el-table-column>
      <el-table-column prop="email" label="电子邮箱" header-align="center"></el-table-column>
      <el-table-column prop="enable" label="是否启用" width="90" align="center">
        <template slot-scope="scope">
          <el-switch
              v-model="scope.row.enable"
              @change="handleChangeEnable(scope.$index, scope.row.id, scope.row.enable)"
              :active-value="1"
              :inactive-value="0"
              active-color="#13ce66"
              inactive-color="#999">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="100" align="center">
        <template slot-scope="scope">
          <el-button circle icon="el-icon-edit" size="mini" @click="handleEdit(scope.row.id)"></el-button>
          <el-button circle icon="el-icon-delete" size="mini" type="danger" @click="openDeleteConfirm(scope.row.id)"></el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: []
    }
  },
  methods: {
    openDeleteConfirm(id) {
      this.$confirm('此操作将永久删除该管理员, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // this.handleDelete(id);
        this.$message.error('尝试删除id=' + id + '的管理员数据，但是，此功能尚未实现！');
      }).catch(() => {
      });
    },
    handleEdit(id) {
      console.log('将编辑id=' + id + "的管理员数据……");
      this.$message.error('尝试编辑id=' + id + '的管理员数据，但是，此功能尚未实现！');
    },
    handleDelete(id) {
      console.log('将删除id=' + id + "的管理员数据……");
      let url = 'http://localhost:9081/admins/' + id + '/delete';
      this.axios.post(url).then((response) => {
        let json = response.data;
        if (json.code == 20000) {
          this.$message({
            message: '删除管理员成功！',
            type: 'success'
          });
        } else {
          this.$message.error(response.data.message);
        }
        this.loadAdmins();
      })
    },
    handleChangeEnable(i, id, toState) {
      let enableText = ['禁用', '启用'];
      let originState = (toState + 1) % 2;
      this.$message.error('尝试将id=' + id + '的管理员状态从【' + enableText[originState] + '】改为【' + enableText[toState] + '】，但是，此功能尚未实现！');
      setTimeout(() => {
        this.tableData[i].enable = originState;
      }, 200);
    },
    loadAdmins() {
      console.log('loadAdmins()');
      let url = 'http://localhost:9081/admins';
      console.log('url = ' + url);
      let jwt = localStorage.getItem('jwt');
      console.log('jwt='+jwt);
      this.axios
          .create({
            headers:{
              'Authorization':jwt
            }
          })
          .get(url).then((response) => {
        let json = response.data;
        if (json.code == 20000) {
          this.tableData = json.data;
        } else {
          this.$message.error(response.data.message);
        }
      });
    }
  },
  created() {
    // created >>> 已创建，将在mounted、显示页面之前执行
    console.log('vue created');
  },
  mounted() {
    // mounted >>> 已挂载，将在created之后、显示页面之前执行
    console.log('vue mounted');
    this.loadAdmins();
  }
}
</script>

<style scoped>

</style>